The Dictionary object contains the following methods:
The Clear method clears all keys and values from the dictionary.
Clear()
Example
The following example resets the dictionary and informs the user.
|
Sub clearDic() Dictionary.Clear() edtMessageBox.Text = "Dictionary has been cleared" End Sub |
The ExportToXml method dumps the data in the dictionary to an XML string.
ExportToXml() As String
Return Value: An XML string containing the contents of the dictionary.
Example
The following example gets an XML string from the dictionary and saves it to the local disk.
|
Sub exportDic() Dim strXml strXml = Dictionary.ExportToXml
'Save XML Set xmlDoc = CreateObject("Msxml2.DOMDocument") xmlDoc.loadXML(strXml) xmlDoc.save("C:\DictionaryData.xml") End Sub |
The FindValue method gets the value from a key.
FindValue(Key As String)
| Parameter | Required | Description |
|---|---|---|
|
Key |
Yes |
A valid dictionary key. |
Return Value: The value of the key.
Example
The following example gets the value for Key1 and displays it in a text box.
|
Sub findValueDic() Dim Ret Ret = Dictionary.FindValue("Key1") edtMessageBox.Text = "The value for Key1 is " & Ret End Sub |
The GetKeyList method retrieves the list of existing keys to allow iteration of the contents of the dictionary.
Note: This method is not exposed in the CygNet Studio-wrapped global Dictionary object.
GetKeyList() as Variant
Return Value: An array of keys in the dictionary.
Example
The following example gets all the keys in the dictionary object and displays them in a list box.
|
Sub getKeyList() Dim objDictionary Set objDictionary = CreateObject("CxScript.Dictionary")
'Get key list Dim arrKeyList, item arrKeyList = objDictionary.GetKeyList()
'Add keys to a list box For Each item in arrKeyList lstDictionary.AddString(item) Next End Sub |
The GetSize method gets the number of keys in the dictionary.
GetSize() As Integer
Return Value: An integer representing the number of keys in the dictionary.
Example
The following example displays the size of the dictionary in a text box.
|
Sub getSizeDic() Dim intSize intSize = Dictionary.GetSize edtMessageBox.Text = "There are " & intSize & " keys in the dictionary" End Sub |
The InitializeFromXml method initializes the dictionary from an XML string.
InitializeFromXml(Xml As String)
| Parameter | Required | Description |
|---|---|---|
|
Xml |
Yes |
A valid dictionary XML string. |
The following is an example of XML that can be used.
|
<Dictionary> <Item Key="Key1">Value1</Item> <Item Key="Key2">Value2</Item> <Item Key="TestKey">Blah</Item> </Dictionary> |
Example
The following example initializes the dictionary from a sample XML string.
|
Sub initXml() Dim strXml strXml = "<Dictionary><Item Key="Key1">Value1</Item</Dictionary>" Dictionary.InitializeFromXml strXml edtMessageBox.Text = "Dictionary initialized" End Sub |
The KeyExists method checks if a key exists.
KeyExists(Key As String) As Boolean
| Parameter | Required | Description |
|---|---|---|
|
Key |
Yes |
The name of a key to check. |
Return Value: A Boolean indicating whether the key exists. If True, the key exists; if False, the key doesn’t exist.
Example
The following example informs the user whether or not TestKey exists.
|
Sub checkKey() Dim bRet bRet = Dictionary.KeyExists("TestKey")
If bRet Then edtMessageBox.Text = "The key exists" Else edtMessageBox.Text = "The key does not exist" End If End Sub |
The Remove method removes an element from the dictionary.
Remove(Key As String)
| Parameter | Required | Description |
|---|---|---|
|
Key |
Yes |
A valid key in the dictionary to remove. |
Example
The following example removes a key from the dictionary.
|
Sub delKey() Dictionary.Remove "Key1" edtMessageBox.Test = "The key has been removed" End Sub |
The SetKeyValue method sets a key-value pair.
SetKeyValue(Key As String, Value As Variant)
| Parameter | Required | Description |
|---|---|---|
|
Key |
Yes |
A valid key in the dictionary. |
|
Value |
Yes |
A new value for the key. This will replace the old value. |
Example
The following example updates a key with a new value.
|
Sub setValue() Dictionary.SetKeyValue "TestKey", "NewValue" edtMessageBox.Text = "TestKey has been updated" End Sub |